package com.xywy.find;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.xywy.R;
import com.xywy.common.BaseActivity;
import com.xywy.common.util.StringUtils;
import com.xywy.common.widget.TitleBar2;
import com.xywy.find.bean.Remind;

/**
 * 提醒 - 类型选择
 * 
 * @author xiaogd
 * 
 */
public class RemindTypeActivity extends BaseActivity {
	private static final String TAG = "RemindTypeActivity";

	private Context mContext;
	private TextView fytx;
	private TextView catx;

	private ImageView ivfytx;
	private ImageView ivcztx;
	/** 药名 */
	private EditText yaoName;
	/** 剂量 */
	private EditText jiliang;

	private TextView tvBack;
	private TextView tvGo;
	private int type = 2; // 1：服药提醒 2:称重提醒

	private RelativeLayout layoutYaoname;

	private RelativeLayout layoutJiliang;

	private Remind remindType;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// 设置为全屏
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.find_remind_fytx);

		this.preInit();
		this.initView();
		this.setLisener();
	}

	@Override
	public void initView() {
		// title
		TitleBar2 titleBar = (TitleBar2) findViewById(R.id.find_remind_fytx_title_bar);
		TextView tvTitle = titleBar.getTitle();
		tvBack = titleBar.getTvBack();
		titleBar.onClickBack(this);
		tvGo = titleBar.getGo();
		tvTitle.setText(getString(R.string.find_remind_title));
		tvBack.setText(getString(R.string.find_remind_add));
		tvGo.setText(getString(R.string.find_remind_save));
		tvGo.setVisibility(View.GONE);

		fytx = (TextView) findViewById(R.id.tv_find_remind_fytx);
		catx = (TextView) findViewById(R.id.tv_find_remind_cztx);
		yaoName = (EditText) findViewById(R.id.et_find_remind_yaoname);
		jiliang = (EditText) findViewById(R.id.et_find_remind_jiliang);
		layoutYaoname = (RelativeLayout) findViewById(R.id.rl_find_yaoname);
		layoutJiliang = (RelativeLayout) findViewById(R.id.rl_find_jiliang);

		ivfytx = (ImageView) findViewById(R.id.iv_find_remind_fytx);
		ivcztx = (ImageView) findViewById(R.id.iv_find_remind_cztx);
	}

	/**
	 * 事件处理
	 * 
	 * @param view
	 */
	public void onClickRemindType(View view) {

		switch (view.getId()) {
		case R.id.rl_find_remind_fytx:
			ivfytx.setVisibility(View.VISIBLE);
			ivcztx.setVisibility(View.INVISIBLE);
			layoutYaoname.setVisibility(View.VISIBLE);
			layoutJiliang.setVisibility(View.VISIBLE);
			type = 1;
			break;
		case R.id.rl_find_remind_cztx:
			ivfytx.setVisibility(View.INVISIBLE);
			ivcztx.setVisibility(View.VISIBLE);
			layoutYaoname.setVisibility(View.GONE);
			layoutJiliang.setVisibility(View.GONE);
			type = 2;
			break;
		}
		;
	}

	@Override
	public void preInit() {
		mContext = this.context;
	}

	@Override
	public void loadData() {
		// remindType = (Remind) getIntent().getSerializableExtra("remindType");
	}

	@Override
	public void onBackPressed() {
		super.onBackPressed();
		this.finish();
	}

	private void setLisener() {
		tvBack.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				backGetData();
			}
		});

		tvGo.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View view) {
				backGetData();
			}
		});
	}

	private void backGetData() {
		// 获取数据
		remindType = (Remind) getIntent().getSerializableExtra("remindType");
		if (remindType == null)
			remindType = new Remind();

		String detail = "";
		String hint = "测量提醒";

		if (type == 1) { // 服药 提醒
			// if (StringUtils.isNull(yaoName.getText().toString())
			// || StringUtils.isNull(jiliang.getText().toString())) {
			// Toast.makeText(mContext,
			// getString(R.string.remind_ym_jl_not_null),
			// Toast.LENGTH_LONG).show();
			// return;
			// detail = yaoName.getText().toString() + ","
			// + jiliang.getText().toString();
			// } else {
			String yaoStr = yaoName.getText().toString();
			String jiliangStr = jiliang.getText().toString();
			if (StringUtils.isNull(jiliangStr)) {
				jiliangStr = "";
				Toast.makeText(mContext, "剂量不能为空", Toast.LENGTH_SHORT).show();
				return;
			}

			if (StringUtils.isNull(yaoStr)) {
				yaoStr = "";
				Toast.makeText(mContext, "药名不能为空", Toast.LENGTH_SHORT).show();
				return;

			}
			detail = yaoStr + "," + jiliangStr;
			// }
			if (isChinese(yaoStr)) {
				if (yaoStr.length() > 8) {
					Toast.makeText(mContext, "请输入8个以下中文字", Toast.LENGTH_SHORT)
							.show();
					return;
				}

			} else {
				if (yaoStr.length() > 16) {
					Toast.makeText(mContext, "请输入16以内字符", Toast.LENGTH_SHORT)
							.show();
					return;
				}
			}
			remindType.setDetail(detail);
			remindType.setType(1);
			hint = getString(R.string.find_remind_fytx);
		} else {
			// // 2:称重提醒
			// if (!StringUtils.isNull(yaoName.getText().toString())
			// || !StringUtils.isNull(jiliang.getText().toString())) {
			// Toast.makeText(mContext,
			// getString(R.string.remind_ym_jl_isright),
			// Toast.LENGTH_LONG).show();
			// return;
			// } else {
			// hint = getString(R.string.find_remind_cztx);
			// }
			remindType.setType(2);
			hint = getString(R.string.find_remind_cztx);
		}
		remindType.setHint(hint);
		// 跳转页面
		Intent intent = new Intent(mContext, RemindAddActivity.class);
		intent.putExtra("remindType", remindType);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		startActivity(intent);
		this.finish();
	}

	public static boolean isChinese(char c) {
		Character.UnicodeBlock ub = Character.UnicodeBlock.of(c);
		if (ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_COMPATIBILITY_IDEOGRAPHS
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_A
				|| ub == Character.UnicodeBlock.CJK_UNIFIED_IDEOGRAPHS_EXTENSION_B
				|| ub == Character.UnicodeBlock.CJK_SYMBOLS_AND_PUNCTUATION
				|| ub == Character.UnicodeBlock.HALFWIDTH_AND_FULLWIDTH_FORMS
				|| ub == Character.UnicodeBlock.GENERAL_PUNCTUATION) {
			return true;
		}
		return false;
	}

	// 完整的判断中文汉字和符号
	public static boolean isChinese(String strName) {
		char[] ch = strName.toCharArray();
		for (int i = 0; i < ch.length; i++) {
			char c = ch[i];
			if (isChinese(c)) {
				return true;
			}
		}
		return false;
	}

}
